package nr.nodehost;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLDecoder;


public class HttpRequestUtils {
    /**
     * post请求
     * @param url         url地址
     * @param jsonParam     参数
     * @param noNeedResponse    不需要返回结果
     * @return  json返回结果
     */
    public static JSONObject httpPost(String url,JSONObject jsonParam, boolean noNeedResponse){
        //  post请求返回结果
        //  DefaultHttpClient httpClient = new DefaultHttpClient();
        CloseableHttpClient httpClient = HttpClients.createDefault();
        JSONObject jsonResult = null;
        HttpPost method = new HttpPost(url);
        try {
            if (null != jsonParam) {
               //解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            url = URLDecoder.decode(url, "UTF-8");
//            请求发送成功，并得到响应结果
            if (result.getStatusLine().getStatusCode() == 200) {
                System.out.println("提交请求至前端");
                String str = "";
                try {
                    /**读取服务器返回过来的json字符串数??**/
                    str = EntityUtils.toString(result.getEntity());
                    //System.out.println(str);
                    if (noNeedResponse) {
                        return null;
                    }
                    //System.out.println(str);
                    /**把json字符串转换成json对象**/
                    jsonResult = JSONObject.fromObject(str);
                } catch (Exception e) {
                    //logger.error("post请求提交失败:" + url, e);
                }
            }
        } catch (IOException e) {
            System.out.println("post请求提交失败:" + e);
        }
        return jsonResult;
    }
    public static void ProgressPost(String url, int result) throws IOException {

        // 创建 HttpClient 实例
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建 POST 请求
        HttpPost httpPost = new HttpPost(url);

        // 设置请求体

        StringEntity entity = new StringEntity(String.valueOf(result));
        httpPost.setEntity(entity);

        // 执行请求
        HttpResponse response = httpClient.execute(httpPost);

        // 获取响应代码
        int responseCode = response.getStatusLine().getStatusCode();
        System.out.println("响应代码：" + responseCode);

        // 你可以根据需要处理响应数据

        // 关闭 HttpClient
        httpClient.close();
    }



    public static void download(String url,String filename) {
        //HttpClient client = new HttpClient();
    	CloseableHttpClient client = HttpClients.createDefault();
    	HttpGet get = new HttpGet(url);
        FileOutputStream output = null;
        
        try {
            //get = new GetMethod("");
            
            CloseableHttpResponse resp=client.execute(get);

            HttpEntity entity=resp.getEntity();
            FileOutputStream os=new FileOutputStream(filename);
            entity.writeTo(os);
            os.close();
            get.releaseConnection();
            client.close();
           
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(output != null){
                    output.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            
            
        }
    }

}
